import $ from '../jquery-3.4.1.min.js';
import ajax from '../utils/ajax';


let communityName;//小区名
let Province;//省
let City;//市
let Area;//区
let input;//输入框具体地址
let communitySite;//小区地址
let communityManager;//小区负责人
let ManagerNumber;//小区负责人电话
let floorSpace;//占地面积
let buildingSpace;//建筑面积
let publicSpace;//公共场所面积
let greeningSpace;//绿化面积
let designParkingSpace;//设计车位面积
let garageSpace;//车库面积
const searchData={value:''};

// 添加小区事件
$('.add-specific-community').click(function () {
    ajax({
        url:'/community',
        type:'GET',
        data:{
            value:searchData.value
        },
        success(res){
            addCommunity(res.result)
        }
    })
    
})

function addCommunity(data){
    if(data.length<5){
        $('#community').hide();
        $('#addCommunity').show();
    }else{
        alert('小区已满，无法添加小区')
    }
}


// 添加小区其他信息界面
$('#addCommunity .next-step').click(function () {
    $('#addCommunity').hide();
    $('.Other-information').show();
    communityName = $('#addCommunity .communityName').val();
    Province = $('#addCommunity .Province').val();
    City = $('#addCommunity .City').val();
    Area = $('#addCommunity .Area').val();
    input = $('#addCommunity #small-input').val();
    communitySite = Province + City + Area + input;
    communityManager = $('#addCommunity .communityManager').val();
    ManagerNumber = $('#addCommunity .ManagerNumber').val();
})
// 点击保存，获取其他信息并发送AJAX
$('.Other-information .save').click(function () {
    $('.Other-information').hide();
    $('#community').show()
    floorSpace = $('.Other-information .floorSpace').val();
    buildingSpace = $('.Other-information .buildingSpace').val();
    publicSpace = $('.Other-information .publicSpace').val();
    greeningSpace = $('.Other-information .greeningSpace').val();
    designParkingSpace = $('.Other-information .designParkingSpace').val();
    garageSpace = $('.Other-information .garageSpace').val();
    console.log(communityName
        , Province
        , City
        , Area
        , input
        , communitySite
        , communityManager
        , ManagerNumber
        , floorSpace
        , buildingSpace
        , publicSpace
        , greeningSpace
        , designParkingSpace
        , garageSpace);
    ajax({
        url: '/community',
        type: 'POST',
        data: {
            communityName
            , Province
            , City
            , Area
            , input
            , communitySite
            , communityManager
            , ManagerNumber
            , floorSpace
            , buildingSpace
            , publicSpace
            , greeningSpace
            , designParkingSpace
            , garageSpace
        },
        success(res) {
            getCommunity()
        }
    })
})

// 默认渲染小区 



getCommunity();
function getCommunity() {
    // console.log(searchData.value);
    ajax({
        url: '/community',
        type: 'GET',
        data:{
            value:searchData.value
        },
        
        success(res) {
            // console.log(res);
            defaultCommunity(res.result)

        }
    })
}

const items01 = require('../../images/items01.png');
const items02 = require('../../images/items02.png');
const items03 = require('../../images/items03.png');
const items04 = require('../../images/items04.png');
const items05 = require('../../images/items05.png');
const items06 = require('../../images/items06.png');
const items07 = require('../../images/items07.png');

function defaultCommunity(data) {
    const html = data.map(item => (
        `
        <div class="specific-community">
            <div class="specific-community-top">
                <div class="specific-item">
                    <div><img src="${items01}" alt=""></div>
                    <div>
                        <h3>${item.communityName}</h3>
                        <p><span>到期时间：</span><span>2021-10-25</span></p>
                    </div>
                </div>
                <div class="specific-item1">
                    <img src="${items02}" alt="">
                    <img src="${items03}" alt="">
                    <img src="${items04}" alt="">
                </div>
            </div>
            <div class="specific-community-bottom">
                <span><img src="${items05}" alt="">续费</span>
                <span><img src="${items06}" alt="">编辑</span>
                <span data-id=${item._id}><img src="${items07}" alt="">删除</span>
            </div>
        </div>
        `
    )).join('');
    $('#community .community-content .specific-community').remove();
    $('#community .community-content .add-specific-community').before(html);
}

// 搜索
$('#community .sousuo').click(function(){
    searchData.value=$(this).prev().val()
    console.log(searchData.value);
    getCommunity();
})
// 小区管理
let _id;
$('#community .community-content').on('click', '.specific-community-bottom span', function () {
    const spanText = $(this).text();
    switch (spanText) {
        case '续费':
            $('#community').hide();
            $('#bayCommunity').show();
            break;
        case '编辑':
            $('#community').hide();
            $('#addCommunity').show();
            break;
        case '删除':
            $('#deletCommunity').show();
            _id=$(this).attr('data-id')
            console.log(_id);
        break;

        default:
            break;
    }
})

// 删除
$('#deletCommunity .deletCommunity-confirm').click(function(){
    $('#deletCommunity').hide();
    ajax({
        url:'/community',
        type:'DELETE',
        data:{_id},
        success(res){
            getCommunity();
        }
    })
})

// module.export={getCommunity}





